package de.openkanban.shared.util;

import java.util.Set;

/**
 * Transforms collections to a String that can be used for a HTTP request or
 * transforms Strings to collections out of a HTTP response.
 * 
 */
public class CollectionStringTransformer {

	private Set<Long> collection;

	private String inputString;

	private final String delimiter = "-";

	public CollectionStringTransformer(Set<Long> input) {
		super();
		this.collection = input;
	}

	public CollectionStringTransformer(Set<Long> collection, String inputString) {
		super();
		this.collection = collection;
		this.inputString = inputString;
	}

	public String toString() {
		String result = "";
		for (Object object : collection) {
			if (object != null) {
				result += object.toString() + delimiter;
			}
		}
		return result.substring(0, result.length() - 1);
	}

	public Set<Long> toCollection() {
		if (inputString != null && inputString.length() > 0) {
			String[] userIdArr = inputString.split(delimiter);
			for (String userId : userIdArr) {
				collection.add(Long.valueOf(userId));
			}

			return collection;
		}
		return null;
	}

}
